package com.github.phoenics.militia.bazookaFx.controller;

import com.github.phoenics.militia.bazookaFx.model.AppModel;
import com.github.phoenics.militia.bazookaFx.model.SchemaTablesViewModel;
import com.github.phoenics.militia.bazookaFx.service.SchemaTablesViewService;
import com.github.phoenics.militia.common.utils.DateUtils;
import de.felixroske.jfxsupport.FXMLController;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Tab;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import org.springframework.beans.factory.annotation.Autowired;
import java.net.URL;
import java.util.Date;
import java.util.ResourceBundle;
import java.util.stream.Collectors;

/**
 * @author Phoenics Chow
 * on 2019年02月08日
 */
@FXMLController
public class SchemaTableViewController implements Initializable {
    private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(SchemaTableViewController.class);
    public final static  ObjectProperty<AppModel> appModel=new SimpleObjectProperty<>();
    //Tab functionTab;
    public final static  ObjectProperty<Tab> functionTabModel=new SimpleObjectProperty<>();
    @FXML
    TableColumn<SchemaTablesViewModel,String> tableSchema;
    @FXML
    TableColumn<SchemaTablesViewModel,String> tableName;
    @FXML
    TableColumn<SchemaTablesViewModel,String> tableComment;
    @FXML
    TableColumn<SchemaTablesViewModel,String> tableType;
    @FXML
    TableColumn<SchemaTablesViewModel, String> createTime;
    @FXML
    TableColumn<SchemaTablesViewModel,String> updateTime;
    @FXML
    TableView schemaTableView;

    @Autowired
    SchemaTablesViewService schemaTablesViewService;

    private final ObservableList<SchemaTablesViewModel> coursData = FXCollections.observableArrayList();
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        schemaTableView.setItems(coursData);
        tableSchema.setCellValueFactory(new PropertyValueFactory<>("tableSchema"));
        tableName.setCellValueFactory(new PropertyValueFactory<>("tableName"));
        tableComment.setCellValueFactory(new PropertyValueFactory<>("tableComment"));
        tableType.setCellValueFactory(new PropertyValueFactory<>("tableType"));
        createTime.setCellValueFactory(new PropertyValueFactory<>("createTime"));
        updateTime.setCellValueFactory(new PropertyValueFactory<>("updateTime"));
        appModel.addListener((ob,o,n)->importData(n.getSchemaName()));
        schemaTableView.setRowFactory(tv->{
            TableRow<SchemaTablesViewModel> row = new TableRow<>();
            row.setOnMouseClicked(event -> {
                if (event.getClickCount() == 2 && (! row.isEmpty()) ) {
                    logger.info(row.getItem().getTableName());
                }
            });
            return row;
        });

    }

    private void importData(String sname){
        coursData.clear();
        coursData.addAll(schemaTablesViewService.listBySchemaTable(sname).stream().map(i->{
            SchemaTablesViewModel schemaTablesViewModel=new SchemaTablesViewModel();
            schemaTablesViewModel.modelWarpper(i,"createTime","updateTime");
            schemaTablesViewModel.setCreateTime(DateUtils.format(i.getCreateTime()));
            schemaTablesViewModel.setUpdateTime(DateUtils.format(i.getUpdateTime()));
            return schemaTablesViewModel;
        }).collect(Collectors.toList()));
    }
    @FXML
    private void refresh(){
        importData(appModel.get().getSchemaName());
    }
    private void importColumnData(String schemaName,String tableName){

    }
    @FXML
    private void syncData(){
        //schemaTablesViewService.syncData(appModel.get());
    }
    @FXML
    private void sampleBuilder(){
        schemaTablesViewService.sampleBuilder(appModel.get());
    }
}
